#include "Sprite.h"

 
Sprite::Sprite(LPD3DXSPRITE SpriteHandler, Texture* Texture, int TimeAnimation, int Index)
	:texture(Texture)
{
	this->_SpriteHandler = SpriteHandler;
	_Index = 0;//Index;
	_TempIndex = Index;
	_frameBegin = _Index;
	_frameEnd = texture->_Count;
	/*_Width = Width;
	_Height = Height;
	_Count = Count;
	_SpritePerRow = SpritePerRow;*/

	//_Texture = new Texture(FileName, Transcolor);

	//_Transcolor = Transcolor;
	_TimeAni = TimeAnimation;
	_TimeLocal = 0;
}

Sprite::~Sprite()
{
	if (texture)
		texture->~Texture();
}

void Sprite::Render(int X, int Y)
{
	RECT srect;

	srect.left = (_Index % texture->_Cols) * texture->_Width + 1;
	srect.top = (_Index / texture->_Cols) * texture->_Height + 1;
	srect.right = srect.left + texture->_Width;
	srect.bottom = srect.top + texture->_Height + 1;

	D3DXVECTOR3 position((float)X, (float)Y, 0);

	_SpriteHandler->Draw(
		texture->Get_Texture(),
		&srect,
		NULL,
		&position,
		D3DCOLOR_XRGB(255, 255, 255));
}


void Sprite::Next()
{
	//_Index = (_Index + _Count + 1) % _Count;
	_Index ++;
	/*if (_Index > texture->_Count - 1)
		_Index = _TempIndex;*/
	if (_Index < _frameBegin)
		_Index = _frameBegin;
	if (_Index > _frameEnd - 1)
		_Index = _frameBegin;
	/*if (_Index < i)
		_Index = texture->_Count - 1;*/
	
}

void Sprite::Reset()
{
	_Index = _frameBegin;
	_TimeLocal = 0;
}

void Sprite::ResetLeft()
{
	_Index = texture->_Count - 1;
	_TimeLocal = 0;
}

void Sprite::Update(int DelTime)
{
	_TimeLocal += DelTime;
	if (_TimeLocal >= _TimeAni)
	{
		_TimeLocal = 0;
		this->Next();
	}

}